package com.recharge.web.controller;

import com.recharge.common.domain.PageDO;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.ResultSupport;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.ParamQueryDO;
import com.recharge.domain.vo.ParamDO;
import com.recharge.domain.vo.UserLoginDO;
import com.recharge.service.ParamService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统参数 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/param")
public class ParamController extends BaseController {

    @Autowired
    private ParamService ParamService;

    @RequiresPermissions("param:add")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    @ApiOperation(value="增加系统参数信息", notes="")
    public @ResponseBody
    ObjectResponse<ParamDO> add(@RequestBody ParamDO domain) {

        ObjectResponse<ParamDO> response = new ObjectResponse<>();

        ResultDO result = ParamService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("param:edit")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    @ApiOperation(value="编辑系统参数信息", notes="")
    public @ResponseBody
    ObjectResponse<ParamDO> edit(@RequestBody ParamDO domain) {
        ObjectResponse<ParamDO> response = new ObjectResponse<>();

        ResultDO result = new ResultSupport();
       try {
            result=ParamService.edit(domain);


       }catch (Exception e){
           e.printStackTrace();
       }
        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("param:param-get")
    @RequestMapping(value="/param.html", method = RequestMethod.POST)
    @ApiOperation(value="获取系统参数信息", notes="")
    public @ResponseBody
    ObjectResponse<ParamDO> get(@RequestBody ParamDO domain) {

        ObjectResponse<ParamDO> response = new ObjectResponse<>();

        ResultDO result = ParamService.get(domain.getKeyed());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((ParamDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("param:delete")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    @ApiOperation(value="删除系统参数信息", notes="")
    public @ResponseBody
    ObjectResponse delete(@RequestBody ParamDO domain) {

        ObjectResponse response = new ObjectResponse();
        UserLoginDO userLoginDO = getUserLoginInfo();

        ResultDO result = ParamService.remove(domain.getKeyed(), userLoginDO.getUsername());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("param:page")
    @RequestMapping(value="/page.html", method = RequestMethod.POST)
    @ApiOperation(value="分页查询系统参数", notes="")
    public @ResponseBody  ObjectResponse<PageDO> page(@RequestBody ParamQueryDO query) {

        ObjectResponse<PageDO> response = new ObjectResponse<>();

        ResultDO result = ParamService.find(query);

        if(result.isSuccess()) {
            response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }

        return response;
    }
}
